﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WGM_Silverlight.Classes;
using System.Windows.Controls.Primitives;
using WGM_Silverlight.Views;

namespace WGM_Silverlight.Controls
{
    public partial class ContactInfo : UserControl
    {
        private bool _isSelected;
        private Color _contactColour;
        private bool _hasNewMessage;
        public WGM_Svc.UserOptions ContactUserOptions { get; private set; }
        private bool _isOnline;
        public bool IsMainWindow { get; private set; }
        private MainView _parent;
        public bool IsOnline
        {
            get { return _isOnline; }
            set 
            { 
                _isOnline = value;
                if (_isOnline)
                {
                    DisplayTextColour = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0));
                }
                else
                {
                    DisplayTextColour = new SolidColorBrush(Colors.Gray);
                }
            }
        }
         
        public ContactInfo(WGM_Svc.UserOptions contactUserOptions, MainView parent)
        {
            InitializeComponent();
            _parent = parent;
            DisplayTextColour = new SolidColorBrush(Colors.Gray);
            if (contactUserOptions != null)
                SetUserOptions(contactUserOptions);
            else
            {
                IsMainWindow = true;
                ContactDisplayName = "Main Conversation";
                ContactDisplayStatus = "group messages";
                ContactUsername = "";
                ContactColour = Color.FromArgb(255, 0, 255, 0);
                DisplayTextColour = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0));
                ToolTipService.SetToolTip(this, "Main Conversation - Group messages appear here");
                ToolTipService.SetPlacement(this, PlacementMode.Top);

            }
            
        }
        public void SetUserOptions(WGM_Svc.UserOptions contactOptions)
        {
            ContactUserOptions = contactOptions;
            ContactDisplayName = contactOptions.DisplayName;
            ContactDisplayStatus = contactOptions.UserStatus;
            ContactUsername = contactOptions.UserName;
            ContactColour = HelperFunctions.GetContactColour(contactOptions);
            
            ToolTipService.SetToolTip(this, HelperFunctions.GetCLToolTip(contactOptions));
            ToolTipService.SetPlacement(this, PlacementMode.Top);
        }
        public bool HasNewMessage 
        {
            get { return _hasNewMessage; }
            set 
            {
                if (!IsSelected)
                {
                    _hasNewMessage = value;
                    if (_hasNewMessage)
                    {
                        Color color = Colors.Orange;
                        _highlightColour.Color = color;
                    }

                }
            } 
        }
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                _isSelected = value;
                if (_isSelected == true)
                {
                    Color color = Colors.Yellow;
                    _highlightColour.Color = color;
                    _backgroundColour.Color = _contactColour;
                    _hasNewMessage = false;
                    
                }
                else
                {
                    _highlightColour.Color = _backgroundColour.Color = Colors.Black;
                }
            }
        }

        public Brush DisplayTextColour
        {
            get { return _contactDisplayName.Foreground; }
            set { _contactDisplayName.Foreground = _contactDisplayStatus.Foreground = value; }
        }

        /// <summary>
        /// The transition color when we hover over the button
        /// </summary>
        public Color TransitionColor
        {
            get
            {
                return transitionColor.Color;
            }
            set
            {
                transitionColor.Color = value;
                transitionSubColor.Color = Color.FromArgb(64, value.R, value.G, value.B);
            }
        }
        public Color ContactColour
        {
            get { return _contactColour; }
            set { _contactColour = value; TransitionColor = value; }
        }
        /// <summary>
        /// The source of the image to display for the control
        /// </summary>
        public string ContactDisplayName
        {
            get { return _contactDisplayName.Text; }
            set { _contactDisplayName.Text = value; }
        }
        public string ContactDisplayStatus
        {
            get { return _contactDisplayStatus.Text; }
            set { _contactDisplayStatus.Text = value; }
        }
        public string ContactUsername
        {
            get;
            set;
        }
        private void LayoutRoot_MouseEnter(object sender, MouseEventArgs e)
        {
            _animEnter.Begin();
        }

        private void LayoutRoot_MouseLeave(object sender, MouseEventArgs e)
        {
            _animLeave.Begin();
        }
        public void ClearAnimation()
        {
            _animLeave.Begin();
        }
    }
}
